package com.dingpeikeji.quickdeer.modules.base.web;

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.Serializable;
import java.net.URLEncoder;

/**
 * Created by wenda on 6/15/2017.
 */
public class AuthUtil {

    public static final String APPID = "wxb2754dc82fc94c5c";
    public static final String APPSECRET = "54cfba7cd5744e23d65ce0828be501d9";
    public static final String QQAPPID = "101408673";
    public static final String QQAPPKEY = "3b3054dc9936f3a33ea98cd514a23c41";
    public static final String WBAPPID = "4151568268";
    public static final String WBAPPKEY = "7cca362a8acdc17625a86dbef7ebd4d6";

    /**
     * send http request and convert resposne into json object
     * @param url
     * @return
     * @throws IOException
     */
    public static JSONObject doGetJson(String url) throws IOException {
        JSONObject jsonObject = null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if(entity !=null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            System.out.println(result);
            jsonObject = JSONObject.fromObject(result);
        }
        httpGet.releaseConnection();
        return jsonObject;
    }
    public static JSONObject doPostJson(String url) throws IOException {
    	JSONObject jsonObject = null;
    	DefaultHttpClient client = new DefaultHttpClient();
    	HttpPost httpPost = new HttpPost(url);
    	HttpResponse response = client.execute(httpPost);
    	HttpEntity entity = response.getEntity();
    	if(entity !=null) {
    		String result = EntityUtils.toString(entity, "UTF-8");
    		System.out.println(result);
    		jsonObject = JSONObject.fromObject(result);
    	}
    	httpPost.releaseConnection();
    	return jsonObject;
    }
    public static String doGetString(String url) throws IOException {
    	DefaultHttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = client.execute(httpGet);
		HttpEntity entity = response.getEntity();
		String result = null;
		if (entity != null) {
			result = EntityUtils.toString(entity, "UTF-8");
			System.out.println(result);
		}
    	httpGet.releaseConnection();
    	return result;
    }
}
